//
//  HBHotCoinChartUtils.m
//  FindClass
//
//  Created by tianlong on 2024/12/4.
//

#import "HBHotCoinChartUtils.h"

@implementation HBHotCoinChartUtils

+ (void)drawCurveInView:(UIView *)view
             withPoints:(NSArray *)points
             curveColor:(UIColor *)color
              lineWidth:(CGFloat)width  {
    if (points.count > 0) {
        // 1. 获取数据中的最大值和最小值（针对y坐标，通过循环遍历查找）
        CGFloat yMax = [[points firstObject][@"y"] floatValue];
        CGFloat yMin = [[points firstObject][@"y"] floatValue];
        for (NSDictionary *point in points) {
            CGFloat currentY = [point[@"y"] floatValue];
            if (currentY > yMax) {
                yMax = currentY;
            }
            if (currentY < yMin) {
                yMin = currentY;
            }
        }
        
        // 2. 创建UIBezierPath对象用于绘制曲线
        UIBezierPath *path = [UIBezierPath bezierPath];
        
        // 设置曲线的起始点，从数组中取出第一个点的坐标，并进行坐标转换，这里对Y轴坐标转换做调整，使其符合常规Y轴向上为正的习惯
        NSDictionary *firstPointDict = points[0];
        CGFloat firstX = [firstPointDict[@"x"] floatValue];
        CGFloat firstY = [firstPointDict[@"y"] floatValue];
        // 将y坐标值从数据范围映射到视图的高度范围，通过视图高度减去转换后的坐标值来反转Y轴方向
        firstY = view.bounds.size.height - (firstY - yMin) / (yMax - yMin) * view.bounds.size.height;
        [path moveToPoint:CGPointMake(firstX, firstY)];
        
        // 循环添加曲线的其他点，使用二次贝塞尔曲线连接，并进行坐标转换，同样对Y轴坐标转换做相应调整
        for (NSInteger i = 1; i < points.count; i++) {
            NSDictionary *currentPointDict = points[i];
            CGFloat currentX = [currentPointDict[@"x"] floatValue];
            CGFloat currentY = [currentPointDict[@"y"] floatValue];
            currentY = view.bounds.size.height - (currentY - yMin) / (yMax - yMin) * view.bounds.size.height;
            
            NSDictionary *prevPointDict = points[i - 1];
            CGFloat prevX = [prevPointDict[@"x"] floatValue];
            CGFloat prevY = [prevPointDict[@"y"] floatValue];
            prevY = view.bounds.size.height - (prevY - yMin) / (yMax - yMin) * view.bounds.size.height;
            
            CGFloat off = 0;//曲线平滑度，值越大越平滑，0=折线
            CGPoint controlPoint1 = CGPointMake((prevX + currentX) / 2, (prevY + currentY) / 2 + off);
            CGPoint controlPoint2 = CGPointMake((prevX + currentX) / 2, (prevY + currentY) / 2 - off);
            [path addCurveToPoint:CGPointMake(currentX, currentY) controlPoint1:controlPoint1 controlPoint2:controlPoint2];
        }
        
        // 3. 创建CAShapeLayer用于显示曲线
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = path.CGPath;
        shapeLayer.strokeColor = color.CGColor;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        shapeLayer.lineWidth = width;
        
        // 4. 将CAShapeLayer添加到视图的图层上
        [view.layer addSublayer:shapeLayer];
    }
}

+ (CALayer *)drawCircleInView:(UIView *)view
                  centerPoint:(CGPoint)centerPoint
                       radius:(CGFloat)radius
                    fillColor:(UIColor *)fillColor
                  borderColor:(UIColor *)borderColor
                  borderWidth:(CGFloat)width {
    // 1. 创建圆的路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint
                                                        radius:radius
                                                    startAngle:0
                                                      endAngle:M_PI * 2
                                                     clockwise:YES];
    
    // 2. 创建CAShapeLayer用于显示圆
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    
    // 3. 设置圆的填充颜色
    shapeLayer.fillColor = fillColor.CGColor;
    
    // 4. 设置圆的边框颜色和边框宽度
    shapeLayer.strokeColor = borderColor.CGColor;
    shapeLayer.lineWidth = width;
    
    return shapeLayer;
}

+ (void)drawLineChartInView:(UIView *)view
                 dataPoints:(NSArray<NSDictionary *> *)dataPoints
                  lineColor:(UIColor *)lineColor
                  lineWidth:(CGFloat)lineWidth {
    // 1. 判断是否有数据点，如果没有则直接返回
    if (!dataPoints || dataPoints.count == 0) {
        return;
    }
    
    // 3. 创建路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    NSDictionary *firstPointDict = dataPoints[0];
    CGFloat firstX = [firstPointDict[@"x"] floatValue];
    CGFloat firstY = [firstPointDict[@"y"] floatValue];
    [path moveToPoint:CGPointMake(firstX, firstY)];
    
    // 4. 遍历数据点，添加线条到路径
    for (NSUInteger i = 1; i < dataPoints.count; i++) {
        NSDictionary *currentPointDict = dataPoints[i];
        CGFloat currentX = [currentPointDict[@"x"] floatValue];
        CGFloat currentY = [currentPointDict[@"y"] floatValue];
        [path addLineToPoint:CGPointMake(currentX, currentY)];
    }
    
    // 5. 创建CAShapeLayer并设置路径和样式
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.strokeColor = lineColor.CGColor;
    shapeLayer.lineWidth = lineWidth;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    
    // 6. 将CAShapeLayer添加到视图的图层上
    [view.layer addSublayer:shapeLayer];
}

+ (NSArray *)findElementsFromArray:(NSArray *)array {
    if (array.count < HotCoinChartXAxisSections) {
        return array;
    }
    // 偶数数组丢弃时间靠前的那一个
    NSArray *tempArr = array.count % 2 == 0 ? [array subarrayWithRange:NSMakeRange(1, array.count - 1)] : array;
    NSMutableArray *arrM = [NSMutableArray array];
    [arrM addObject:tempArr.firstObject];
    
    NSInteger midIdx = tempArr.count / 2;
    
    NSArray *midLeftArr = [tempArr subarrayWithRange:NSMakeRange(1, midIdx - 1)];
    id midLeftObj = midLeftArr[midLeftArr.count / 2];
    [arrM addObject:midLeftObj];
    
    id midObj = tempArr[midIdx];
    [arrM addObject:midObj];
    
    NSArray *midRightArr = [tempArr subarrayWithRange:NSMakeRange(midIdx, midIdx - 1)];
    id midRightObj = midRightArr[midRightArr.count / 2];
    [arrM addObject:midRightObj];
    
    [arrM addObject:tempArr.lastObject];
    
    return [NSArray arrayWithArray:arrM];
}

+ (UIColor *)randomColor {
    CGFloat red = (CGFloat)arc4random_uniform(256)/255.0;
    CGFloat green = (CGFloat)arc4random_uniform(256)/255.0;
    CGFloat blue = (CGFloat)arc4random_uniform(256)/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

@end
